package com.edu.etu.gui.checkit;


import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelperList extends SQLiteOpenHelper{

    public static final String NameTableLists = "Lists";
    public String nameLists = "nameLists";

    public static final String NameTableElements = "Elements";
    public String idList = "idList";//id названия листа в таблице
    public String nameElements = "nameElements";//имя элемента листа
    public String boolElements = "boolElements";//true false

    public static final String NameTableListsTemplate = "ListsTemplates";
    public String nameListsTemplates = "nameListsTemplates";

    public static final String NameTableTemplate = "Template";
    public String idListsTemplates = "ListsTemplates";
    public String nameElementsTemplate = "nameElementsTemplate";//имя элемента листа
    public String boolTemplate = "boolTemplate";//true false

    private String TableLists = "create table "+ NameTableLists +"(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+nameLists+" TEXT)";
    private String TableElements = "create table "+ NameTableElements +"(_id2 INTEGER PRIMARY KEY AUTOINCREMENT,"+idList+" INTEGER,"+nameElements+" TEXT,"+boolElements+" INTEGER)";
    private String TableListsTemplate = "create table "+ NameTableListsTemplate +"(_id4 INTEGER PRIMARY KEY AUTOINCREMENT,"+nameListsTemplates+" TEXT)";
    private String TableTemplate = "create table "+ NameTableTemplate +"(_id3 INTEGER PRIMARY KEY AUTOINCREMENT,"+idListsTemplates+" INTEGER,"+nameElementsTemplate+" TEXT,"+boolTemplate+" INTEGER)";
    Context inputContext;
    public SQLiteHelperList(Context context) {
        super(context, "DataBase_Apps_Check_It", null, 1);
        inputContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        Log.d("myLog", " == onCreate == ");
        database.execSQL(TableLists);
        database.execSQL(TableElements);
        database.execSQL(TableListsTemplate);
        database.execSQL(TableTemplate);
        ContentValues cv=new ContentValues();

        cv.put(nameListsTemplates,inputContext.getString(R.string.Shop));
        int id = (int)database.insert(NameTableListsTemplate,null,cv);
        cv.clear();
        addTemplateElement(database,id,R.string.Bread);
        addTemplateElement(database,id,R.string.Sausage);
        addTemplateElement(database,id,R.string.Milk);
        addTemplateElement(database,id,R.string.Beer);
        addTemplateElement(database,id,R.string.Eggs);
        addTemplateElement(database,id,R.string.Tea);
        addTemplateElement(database,id,R.string.Coffee);
        addTemplateElement(database,id,R.string.Meat);
        addTemplateElement(database,id,R.string.Juice);
        addTemplateElement(database,id,R.string.Oil);
        addTemplateElement(database,id,R.string.Fruits);
        addTemplateElement(database,id,R.string.Potato);
        addTemplateElement(database,id,R.string.Tomato);
        addTemplateElement(database,id,R.string.Cucumber);
        addTemplateElement(database,id,R.string.Cabbage);
        addTemplateElement(database,id,R.string.Chips);
        addTemplateElement(database,id,R.string.Ice_Cream);
    }
    public void addTemplateElement (SQLiteDatabase database,int id,int element)
    {
        ContentValues cv2=new ContentValues();
        cv2.put(idListsTemplates, id);
        cv2.put(nameElementsTemplate, inputContext.getString(element));
        cv2.put(boolTemplate, 0);
        database.insert(NameTableTemplate, null, cv2);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("myLog", " == onUpgrade == ");

        onCreate(db);
    }
}
